package net.ib.mtalk.ui.inbox;

import java.util.ArrayList;
import java.util.UUID;

import net.ib.mntalk.protocol.Common;
import net.ib.mtalk.R;
import net.ib.mtalk.base.BaseFragmentActivity;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.DatabaseContract.ViewTable;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.ui.dialog.DialogListItem;
import net.ib.mtalk.ui.dialog.DialogMenu;
import net.ib.mtalk.ui.dialog.DialogMenuOnClickListener;
import net.ib.mtalk.ui.fragment.InboxListFragment;
import net.ib.mtalk.ui.setting.SettingsActivity;
import net.ib.mtalk.util.MTalkLogWriter;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class InboxListActivity extends BaseFragmentActivity implements DialogMenuOnClickListener {

	public static final int MENU_PARTICIPANTS 			= Menu.FIRST + 9;
	public static final int MENU_PRIVACY 				= Menu.FIRST + 10;
	public static final int MENU_EXCLUDE 				= Menu.FIRST + 11;
	public static final int MENU_LEAVE_CHAT				= Menu.FIRST + 12;
	public static final int MENU_RENAME 				= Menu.FIRST + 13;
	public static final int MENU_PUSH 					= Menu.FIRST + 14;
	public static final int MENU_BACKUP_HISTORY_EMAIL 	= Menu.FIRST + 15;
	public static final int MENU_BLOCK 					= Menu.FIRST + 16;
	public static final int MENU_ADD 					= Menu.FIRST + 17;
	public static final int MENU_INFO 					= Menu.FIRST + 18;
	public static final int MENU_UNLOCK 				= Menu.FIRST + 19;
	public static final int MENU_REMOVE					= Menu.FIRST + 20;
	
	public static final int GET_VIEWS_COUNT				= 50;
	
	long mClientViewUuidMsb;
	long mClientViewUuidLsb;
	
	private InboxController mController;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case HandlerMsgConst.HANDLER_ADD_MEMBERS_SUCCESS:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_ADD_MEMBERS_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_ADD_MEMBERS_FAIL:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_ADD_MEMBERS_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_UNKNOWN_ERROR:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_UNKNOWN_ERROR", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_MEMBERS_SUCCESS:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_MEMBERS_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_MEMBERS_FAIL:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_MEMBERS_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_VIEW_PROPERTIES_SUCCESS:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_VIEW_PROPERTIES_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_VIEW_PROPERTIES_FAIL:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_VIEW_PROPERTIES_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_VIEWS_CHANGES_SUCCESS:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_VIEWS_CHANGES_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_VIEWS_CHANGES_FAIL:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_VIEWS_CHANGES_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_VIEWS_SUCCESS:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_VIEWS_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_VIEWS_FAIL:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_GET_VIEWS_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_REMOVE_MEMBERS_SUCCESS:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_REMOVE_MEMBERS_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_REMOVE_MEMBERS_FAIL:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_REMOVE_MEMBERS_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_REMOVE_VIEW_SUCCESS:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_REMOVE_VIEW_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_REMOVE_VIEW_FAIL:
				// TODO
				Toast.makeText(InboxListActivity.this, "HANDLER_REMOVE_VIEW_FAIL", Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inbox_list);

		mController = new InboxController(this, mHandler);

		FragmentManager fm = getSupportFragmentManager();
		
		if (fm.findFragmentById(R.id.inbox_list) == null) {
			InboxListFragment list = new InboxListFragment();
			fm.beginTransaction().add(R.id.inbox_list, list).commit();
		}
		
		// TODO
		// 1. REQUEST TO SERVER
		//mController.getViews();
		// 3. SEND ACK, THEN CRUD TO DATABASE
	}

	
	@Override
	public void onResume() {
		super.onResume();
		// getViews는 최초 인증시에만 불러온다.
//		mController.getAllViews();
//		mController.getViewChangeForNoti();
	}
	
	public void showDialogMenu(int chatType, int pushType, int enableType, long clientViewUuidMsb, long clientViewUuidLsb){
		mClientViewUuidMsb = clientViewUuidMsb;
		mClientViewUuidLsb = clientViewUuidLsb;
		DialogMenu dialogMenu = new DialogMenu(this, getMenuList(chatType, pushType, enableType));
		dialogMenu.createDialog().show();
	}
	
	// 메뉴 클릭 아이템
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, R.string.settings).setIcon(R.drawable.menu_icon_settings);
//		menu.add(1, 1, 1, "ViewChanged");
		return true;
	}

	// 메뉴 선택
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()){
		case 0:
			Intent intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
			break;
//		case 1:
//			mController.getViewChangeTest();
//			break;
		default:
		}
		return true;
	}

	@Override
	public boolean onClickDailogMenu(Object obj, int tag){
//		ArrayList<DialogListItem> items = (ArrayList<DialogListItem>) obj;
		DialogListItem item = (DialogListItem) obj;
			
		switch(tag){
			case MENU_ADD:
				MTalkLogWriter.d("MENU_ADD");
				break;
			case MENU_BACKUP_HISTORY_EMAIL:
				MTalkLogWriter.d("MENU_BACKUP_HISTORY_EMAIL");
				// TODO Backup message history Email
				mController.backupHistoryMessage(new UUID(mClientViewUuidMsb, mClientViewUuidLsb));
				break;
			case MENU_BLOCK:
				MTalkLogWriter.d("MENU_BLOCK");
				break;
			case MENU_EXCLUDE:
				MTalkLogWriter.d("MENU_EXCLUDE");
				break;
			case MENU_INFO:
				MTalkLogWriter.d("MENU_INFO");
				break;
			case MENU_LEAVE_CHAT:
				mController.leaveChat(new UUID(mClientViewUuidMsb, mClientViewUuidLsb));
				MTalkLogWriter.d("MENU_LEAVE_CHAT");
				break;
			case MENU_PARTICIPANTS:
				MTalkLogWriter.d("MENU_PARTICIPANTS");
				break;
			case MENU_PRIVACY:
				MTalkLogWriter.d("MENU_PRIVACY");
				break;
			case MENU_PUSH:
				if(item.text.equals(getString(R.string.dialog_item_push_off_text))){					mController.updateViewPush(ViewTable.PushType.OFF.ordinal(), mClientViewUuidMsb, mClientViewUuidLsb);				}else if(item.text.equals(getString(R.string.dialog_item_push_on_text))){
					mController.updateViewPush(ViewTable.PushType.ON.ordinal(), mClientViewUuidMsb, mClientViewUuidLsb);				}
				break;
			case MENU_REMOVE:
				switch(mController.getChatType(mClientViewUuidMsb, mClientViewUuidLsb)){
					case Common.GROUP_CHAT:
						mController.deleteViewGroup(mClientViewUuidMsb, mClientViewUuidLsb);
						break;
					case Common.BROADCAST:
						mController.deleteViewBroadcast(mClientViewUuidMsb, mClientViewUuidLsb);
						break;
					case Common.ONE_TO_ONE:
						mController.deleteViewOnetoone(mClientViewUuidMsb, mClientViewUuidLsb);
						break;
					default:
						mController.deleteViewOnetoone(mClientViewUuidMsb, mClientViewUuidLsb);
						break;
				}
				break;
			case MENU_RENAME:
				final EditText renameText = new EditText( this );
				new AlertDialog.Builder(InboxListActivity.this)
				.setTitle(this.getString(R.string.inbox_title_rename))
				.setView(renameText)
				.setPositiveButton( R.string.ok, new DialogInterface.OnClickListener(){
					public void onClick( DialogInterface dialog, int whichButton ){
						if(!renameText.getText().toString().equals("")){
							mController.updateViewTitle(renameText.getText().toString(), mClientViewUuidMsb, mClientViewUuidLsb);
						}
					}})
				.setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener(){
					public void onClick( DialogInterface dialog, int whichButton ){
						dialog.cancel();
					}
				}).show();
				MTalkLogWriter.d("MENU_RENAME");
				break;
			case MENU_UNLOCK:
				MTalkLogWriter.d("MENU_UNLOCK");
				break;
			default:
				MTalkLogWriter.d("MENU_DEFAULT : !TODO - MUST ITEM ADD (" + tag + ")");
				break;
		}
		return true;
	}
		private ArrayList<DialogListItem> getMenuList(int chatType, int pushState, int enableType){
		ArrayList<DialogListItem> item = new ArrayList<DialogListItem>();
		if(chatType == Common.ONE_TO_ONE){	// 1:1 채팅일때 다이얼 로그 아이템
			item.add(new DialogListItem(this.getResources(),
					R.string.dialog_item_backup_message_email_text, 
					R.drawable.dialog_item_backup_message_email_img, 
					MENU_BACKUP_HISTORY_EMAIL));
		} else if(chatType == Common.BROADCAST){ // 단체 메세지 일때 다이얼 로그 아이템
			item.add(new DialogListItem(this.getResources(), 
					R.string.dialog_item_rename_title_text, 
					R.drawable.dialog_item_rename_title_img, 
					MENU_RENAME));
		} else if(chatType == Common.GROUP_CHAT){ // 그룹 채팅일때 다이얼 로그 아이템
			// push on/off 확인
			if(pushState == ViewTable.PushType.OFF.ordinal()){
				item.add(new DialogListItem(this.getResources(), 
						R.string.dialog_item_push_on_text, 
						R.drawable.dialog_item_push_on_img, 
						MENU_PUSH));				
			}else if(pushState == ViewTable.PushType.ON.ordinal()){
				item.add(new DialogListItem(this.getResources(), 
						R.string.dialog_item_push_off_text, 
						R.drawable.dialog_item_push_off_img, 
						MENU_PUSH));
			}
			item.add(new DialogListItem(this.getResources(), 
					R.string.dialog_item_rename_title_text, 
					R.drawable.dialog_item_rename_title_img, 
					MENU_RENAME));
			item.add(new DialogListItem(this.getResources(), 
					R.string.dialog_item_backup_message_email_text, 
					R.drawable.dialog_item_backup_message_email_img, 
					MENU_BACKUP_HISTORY_EMAIL));
			if(enableType != ViewTable.EnableType.LEAVE.ordinal()){
				item.add(new DialogListItem(this.getResources(), 
						R.string.dialog_item_leave_chat_text, 
						R.drawable.dialog_item_leave_chat_img, 
						MENU_LEAVE_CHAT));
			}
		}
		// 공통 다이얼 로그 아이템
		item.add(new DialogListItem(this.getResources(), 
				R.string.dialog_item_remove_text, 
				R.drawable.dialog_item_remove_img, 
				MENU_REMOVE));
		return item;
	}	
}
